define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'catalog/crawl/index',
                    add_url: 'catalog/crawl/add',
                    edit_url: '',
                    del_url: 'catalog/crawl/del',
                    multi_url: 'catalog/crawl/multi',
                    table: 'crawl',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'url', title: __('采集URL'), operate: 'LIKE',align:'left'},
                        {field: 'email_count', title: __('email count'), operate: false,align:'left'},
                        {field: 'pages', title: __('Pages'), operate: false,align:'left'},
                        {field: 'author', title: __('用戶名稱'), operate: false,align:'left'},
                        {
                            field: 'created_at',
                            title: __('Createtime'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
                            width: 150
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                        buttons: [
                            {
                                name: 'detail',
                                text: '',
                                title:'Emails',
                                icon: 'fa fa-list',
                                classname: 'btn btn-primary btn-xs btn-dialog',
                                url: 'catalog/crawl_email/index?crawl_id={ids}',
                            },
                        ],    
                        formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});